﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.Charence.PassManager.BusinessEntities;
using Com.Charence.PassManager.BusinessEntities.Account;
using Com.Charence.PassManager.Common;
using Com.Charence.PassManager.Common.Extensions;

namespace Com.Charence.PassManager.DataComponents
{
    public class AccountDC
    {
        public void SaveNewAccount(SaveAccountRequest request)
        {
            using (var context = new Entities())
            {
                var accountBase = context.AccountBases.Add(new AccountBase
                {
                    Owner = AppContext.Current.LoginUserID,
                    Account = request.Account,
                    Password = request.Password.Encrypt(),
                    WebSiteName = request.AccountOwner,
                    IsDeleted = false,
                    CreatedBy = AppContext.Current.LoginUserInfo.UserAccount,
                    CreatedOn = DateTime.Now,
                    UpdatedBy = AppContext.Current.LoginUserInfo.UserAccount,
                    UpdatedOn = DateTime.Now
                });

                for (var i = 0; i < request.SecureQuestions.Count; i++)
                {
                    context.AccountProperties.Add(new AccountProperty
                    {
                        AccountID = accountBase.ID,
                        PropertyTypeID = GetSecQID(i),
                        PropertyValue = request.SecureQuestions[i].ToXml(),
                        IsDeleted = false,
                        CreatedBy = AppContext.Current.LoginUserInfo.UserAccount,
                        CreatedOn = DateTime.Now,
                        UpdatedBy = AppContext.Current.LoginUserInfo.UserAccount,
                        UpdatedOn = DateTime.Now
                    });
                }

                foreach (var item in request.Properties)
                {
                    context.AccountProperties.Add(new AccountProperty
                    {
                        AccountID = accountBase.ID,
                        PropertyTypeID = Constants.PropertyType.Custom,
                        PropertyName = item.Key,
                        PropertyValue = item.Value,
                        IsDeleted = false,
                        CreatedBy = AppContext.Current.LoginUserInfo.UserAccount,
                        CreatedOn = DateTime.Now,
                        UpdatedBy = AppContext.Current.LoginUserInfo.UserAccount,
                        UpdatedOn = DateTime.Now
                    });
                }

                context.SaveChanges();
            }
        }

        private int GetSecQID(int senquence)
        {
            switch (senquence)
            {
                case 0:
                    return Constants.PropertyType.SecureQuestion1;
                case 1:
                    return Constants.PropertyType.SecureQuestion2;
                case 2:
                    return Constants.PropertyType.SecureQuestion3;
                default:
                    return Constants.PropertyType.Custom;
            }
        }

        public List<AccountBase> GetAccountGrid(List<string> keywords)
        {
            using (var context = new Entities())
            {
                var result = from a in context.AccountBases
                             where !a.IsDeleted && a.Owner == AppContext.Current.LoginUserID
                             select a;
                foreach (var keyword in keywords)
                {
                    result = result.Where(p => p.WebSiteName.Contains(keyword) || p.Account.Contains(keyword));
                }
                return result.ToList();
            }
        }
    }
}
